﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Common
{
    public class PrjSet
    {
        public string typeName;
        public int cardCount;
        public int channelCount;
    }

    public  class Cls_SystemSet
    {
        XmlNode databasenode=null;
        XmlNode lognode = null;
        XmlNode databasenodeother = null;
        XmlNode prjset = null;
        XmlNode autoset = null;
      
        public Cls_SystemSet()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load( "SystemSet.xml");
            databasenode = xmlDoc.SelectSingleNode("SysSet").SelectSingleNode("DatabaseSet");
            databasenodeother = xmlDoc.SelectSingleNode("SysSet").SelectSingleNode("DatabaseSetOther");
            lognode = xmlDoc.SelectSingleNode("SysSet").SelectSingleNode("LogSet");
            autoset = xmlDoc.SelectSingleNode("SysSet").SelectSingleNode("AutoSet");

        }


        public string type
        {
            get
            {
                return databasenode["type"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenode.OwnerDocument;
                XmlNode x = databasenode["type"];
                if (null == x)
                    x = databasenode.AppendChild(doc.CreateElement("type"));
                databasenode["type"].InnerText = value.ToString();
            }
        }



        public string DataSource
        {
            get
            {
                return databasenode["DataSource"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenode.OwnerDocument;
                XmlNode x = databasenode["DataSource"];
                if (null == x)
                    x = databasenode.AppendChild(doc.CreateElement("DataSource"));
                databasenode["DataSource"].InnerText = value.ToString();
            }
        }

        public string InitialCatalog
        {
            get
            {
                return databasenode["InitialCatalog"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenode.OwnerDocument;
                XmlNode x = databasenode["InitialCatalog"];
                if (null == x)
                    x = databasenode.AppendChild(doc.CreateElement("InitialCatalog"));
                databasenode["InitialCatalog"].InnerText = value.ToString();
            }
        }

        public string UserID
        {
            get
            {
                return databasenode["UserID"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenode.OwnerDocument;
                XmlNode x = databasenode["UserID"];
                if (null == x)
                    x = databasenode.AppendChild(doc.CreateElement("UserID"));
                databasenode["UserID"].InnerText = value.ToString();
            }
        }

        public string Password
        {
            get
            {
                return databasenode["Password"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenode.OwnerDocument;
                XmlNode x = databasenode["Password"];
                if (null == x)
                    x = databasenode.AppendChild(doc.CreateElement("Password"));
                databasenode["Password"].InnerText = value.ToString();
            }
        }


        public string typeother
        {
            get
            {
                return databasenodeother["type"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenodeother.OwnerDocument;
                XmlNode x = databasenodeother["type"];
                if (null == x)
                    x = databasenodeother.AppendChild(doc.CreateElement("type"));
                databasenodeother["type"].InnerText = value.ToString();
            }
        }

        public string DataSourceother
        {
            get
            {
                return databasenodeother["DataSource"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenodeother.OwnerDocument;
                XmlNode x = databasenodeother["DataSource"];
                if (null == x)
                    x = databasenodeother.AppendChild(doc.CreateElement("DataSource"));
                databasenodeother["DataSource"].InnerText = value.ToString();
            }
        }

        public string InitialCatalogother
        {
            get
            {
                return databasenodeother["InitialCatalog"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenodeother.OwnerDocument;
                XmlNode x = databasenodeother["InitialCatalog"];
                if (null == x)
                    x = databasenodeother.AppendChild(doc.CreateElement("InitialCatalog"));
                databasenodeother["InitialCatalog"].InnerText = value.ToString();
            }
        }

        public string UserIDother
        {
            get
            {
                return databasenodeother["UserID"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenodeother.OwnerDocument;
                XmlNode x = databasenodeother["UserID"];
                if (null == x)
                    x = databasenodeother.AppendChild(doc.CreateElement("UserID"));
                databasenodeother["UserID"].InnerText = value.ToString();
            }
        }

        public string Passwordother
        {
            get
            {
                return databasenodeother["Password"].InnerText;
            }
            set
            {
                XmlDocument doc = databasenodeother.OwnerDocument;
                XmlNode x = databasenodeother["Password"];
                if (null == x)
                    x = databasenodeother.AppendChild(doc.CreateElement("Password"));
                databasenodeother["Password"].InnerText = value.ToString();
            }
        }



        public string DBConnectionString()
        {
            if (type == "0")
            {
                return "Data Source=" + DataSource +
                 ";Initial Catalog=" + InitialCatalog +
                 ";Persist Security Info=True;User ID=" + UserID +
                 ";Password=" + Password + "";
            }
            else
            {
                return "Data Source=" + DataSource +
                          ";Initial Catalog=" + InitialCatalog +
                          ";Integrated Security=True";
            }
  
        }


        public string DBConnectionStringOther()
        {
            if (typeother == "0")
            {
                return "Data Source=" + DataSourceother +
                 ";Initial Catalog=" + InitialCatalogother +
                 ";Persist Security Info=True;User ID=" + UserIDother +
                 ";Password=" + Passwordother + "";
            }
            else
            {
                return "Data Source=" + DataSourceother +
                          ";Initial Catalog=" + InitialCatalogother +
                          ";Integrated Security=True";
            }

        }


        public string LogPath
        {
            get
            {
                return lognode["LogPath"].InnerText;
            }
            set
            {
                XmlDocument doc = lognode.OwnerDocument;
                XmlNode x = lognode["LogPath"];
                if (null == x)
                    x = lognode.AppendChild(doc.CreateElement("LogPath"));
                lognode["LogPath"].InnerText = value.ToString();
            }
        }


        public string ProjectName 
        {
            get
            {
                try
                {


                    return autoset["ProjectName"].InnerText;
                }
                catch (Exception)
                {

                    return "";
                }
            }
            set
            {
                XmlDocument doc = autoset.OwnerDocument;
                XmlNode x = autoset["ProjectName"];
                if (null == x)
                    x = autoset.AppendChild(doc.CreateElement("ProjectName"));
                autoset["ProjectName"].InnerText = value.ToString();
            }
        }

        public string UserName
        {
            get
            {
                try
                {


                    return autoset["UserName"].InnerText;
                }
                catch (Exception)
                {

                    return "";
                }
            }
            set
            {
                XmlDocument doc = autoset.OwnerDocument;
                XmlNode x = autoset["UserName"];
                if (null == x)
                    x = autoset.AppendChild(doc.CreateElement("UserName"));
                autoset["UserName"].InnerText = value.ToString();
            }
        }


        public string Pwd
        {
            get
            {
                try
                {


                    return autoset["Pwd"].InnerText;
                }
                catch (Exception)
                {

                    return "";
                }
            }
            set
            {
                XmlDocument doc = autoset.OwnerDocument;
                XmlNode x = autoset["Pwd"];
                if (null == x)
                    x = autoset.AppendChild(doc.CreateElement("Pwd"));
                autoset["Pwd"].InnerText = value.ToString();
            }
        }


        public string IsAuto
        {
            get
            {
                try
                {


                    return autoset["IsAuto"].InnerText;
                }
                catch (Exception)
                {

                    return "0";
                }
            }
            set
            {
                XmlDocument doc = autoset.OwnerDocument;
                XmlNode x = autoset["IsAuto"];
                if (null == x)
                    x = autoset.AppendChild(doc.CreateElement("IsAuto"));
                autoset["IsAuto"].InnerText = value.ToString();
            }
        }

        public List<PrjSet> GetPrjList() 
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("SystemSet.xml");
            List<PrjSet> prjlist = new List<PrjSet>();
            prjset = xmlDoc.SelectSingleNode("SysSet").SelectSingleNode("PrjSet");
            for (int i = 0; i < prjset.ChildNodes.Count;i++ )
            {
                PrjSet ps = new PrjSet();
                ps.typeName = prjset.ChildNodes[i]["Name"].InnerText;
                ps.cardCount = Convert.ToInt32(prjset.ChildNodes[i]["Card"].InnerText);
                ps.channelCount = Convert.ToInt32(prjset.ChildNodes[i]["Channel"].InnerText);
                prjlist.Add(ps);
            }
            return prjlist;
        }


        public PrjSet GetPrjSet(int TypeId)
        {
            return GetPrjList()[TypeId];
        }
    }
}
